<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
	<script type="text/javascript" src="/js/libs/jquery-2.1.1.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
	<script src="/js/bootstrap/bootstrap.min.js"></script>
	<script src="/js/bootstrap/bootstrap-select.min.js"></script>
	<script type="text/javascript" src="/js/jq.js"></script>
	<script type="text/javascript" src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="/js/common.js"></script>
	<script type="text/javascript" src="/layui/layui.js"></script>
	<script type="text/javascript" src="/js/dict.js"></script>
	<script type="text/javascript" src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
	<script type="text/javascript" src="/js/my/ztree-menu.js"></script>
	<script type="text/javascript" src="/js/my/permission.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
			<div class='form-group'>
				<label class='col-md-4 control-label'>商品</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='isCommon' id='isCommon' >
					<input class='form-control' type='hidden' name='goodsId' id='goodsId' >
					<input style="width: 90%;float: left;" class='form-control' placeholder='商品' type='text' name='goodsName' id='goodsName' readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showTableAndFlushFields('fas/goods','goods', '商品', true, false, 'goodsId:id', 'goodsName:name', 'isCommon:isCommon')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group' name="pDiv">
				<label class='col-md-4 control-label'>人员</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='userId' id='userId'>
					<input style="width: 90%;float: left;" class='form-control' placeholder='人员' type='text'
						   name='userName' id='userName' readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showTableAndFlushFields('user', 'user', '用户', true, false, 'userId:id', 'userName:nickname','deptId:deptId', 'deptName:deptName')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group' name="dDiv">
				<label class='col-md-4 control-label'>所属部门</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='deptId' id='deptId' >
					<input style="width: 90%;float: left;" class='form-control' placeholder='所属部门' type='text' name='deptName' id='deptName' readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showSpecialTree('deptId', 'deptName', 'dept', '部门', 'root', 'select','fasDeptTree', true, true)">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group' name="cDiv">
				<label class='col-md-4 control-label'>所属公司</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="company" id="company"
							title="所属公司" data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>总数量</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='总数量' type='text' name='totalNumber' id='totalNumber'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>备注</label>
				<div class='col-md-8'>
					<textarea class="form-control" placeholder="备注" name="remark" id="remark"></textarea>
				</div>
			</div>
			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<button class="btn btn-primary" type="submit" onclick="save()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>
		</fieldset>
	</form>
</div>
<script type="text/javascript">
	$(function () {
		$('#form').bootstrapValidator();
		showDictSelect('company', 'company', '', 'beep');
	});

	layui.use(['layer','laydate'], function(){
		var layer = layui.layer;
	});

	// 设置验证规则
	var validatorSettings = {
		message: '验证不通过',
		fields: {
			userName: {
				message: '所属人员验证失败',
				validators: {
					callback:{
						message:'修改非常规商品时人员不能为空',
						callback:function(value,validator){
							var userName = validator.getFieldElements('userName').val();
							var isCommon = validator.getFieldElements('isCommon').val();
							if(isCommon != "" && isCommon == "0" && userName == ""){
								return false;
							}else{
								return true;
							}
						}
					}
				}
			},
			deptName: {
				message: '所属部门验证失败',
				validators: {
					notEmpty: {
						message: '所属部门不能为空'
					}
				}
			},
			goodsName: {
				message: '商品验证失败',
				validators: {
					notEmpty: {
						message: '商品不能为空'
					}
				}
			},
			totalNumber: {
				message: '总数量验证失败',
			    validators: {
			        notEmpty: {
			            message: '总数量不能为空'
			        },
					numeric: {
						message: '总数量只能输入数字'
					},
					regexp: {
						regexp: /^[0-9-]+$/,
						message: '总数量不能输入小数'
					},
					callback:{
						message: '不能输入0',
						callback: function(value,validator) {
							value = parseInt(value);
							if(value == 0){
								return false;
							}else{
								return true;
							}
						}
					}
			    }
			}
		},
		submitHandler: function (validator, form, submitButton) {
			alert("submit");
		}
	};

	$('#form').bootstrapValidator(validatorSettings);

	function save() {
		$('#form').bootstrapValidator(validatorSettings);
		var bootstrapValidator = $("#form").data('bootstrapValidator');
		bootstrapValidator.validate();
		if(!bootstrapValidator.isValid()){
		   return;
		}

		var formdata = $("#form").serializeObject();

		$.ajax({
			type : 'post',
			url : '/surplusLosses/',
			contentType: "application/json; charset=utf-8",
			data : JSON.stringify(formdata),
			success : function(data) {
				if (data.code === 0) {
					layer.msg("添加成功", {shift: -1, time: 1000}, function () {
						parent.location.reload();//关闭iFrame
					});
				} else {
					layer.msg("添加失败：" + data.message, {
						shift: -1,
						time: 20000,
						btn: ['知道了'],//按钮
						btnAlign: 'c' //按钮居中
					});
				}
			}
		});


	}

</script>
</body>
</html>
